<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExportUp">导出（上表）</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExportDown('#badAnalyseChild', '不良品分析报表（下表）')">
          导出（下表）
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">进行</Tag>
      <Tag type="dot" size="default" color="#bb0331">等待</Tag>
      <Tag type="dot" size="default" color="#61b2e0">暂停</Tag>
      <Tag type="dot" size="default" color="#606266">完成</Tag>
      <el-col :span="2.2" style="position: absolute; right: -10px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 上表 -->
    <el-table
      v-loading="loading"
      highlight-current-row
      border
      stripe
      height="400px"
      :row-style="{ padding: '0px' }"
      :cell-style="iCellStyle"
      :header-row-style="{ padding: '2px' }"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="badAnalyseList"
      @row-click="handleClick">
      <el-table-column label="#" type="index" width="55" align="center" />
      <el-table-column label="制造单号" width="120" align="center" prop="moeZzdh" />
      <el-table-column label="销售单号" width="120" align="center" prop="moeSodh" />
      <el-table-column label="模具编号" width="200" align="center" prop="moeMjbh" show-overflow-tooltip />
      <el-table-column label="物料代码" width="120" align="center" prop="moeWldm" />
      <el-table-column label="品名规格" width="400" align="center" prop="itmPmgg" show-overflow-tooltip />
      <el-table-column label="机台" width="60" align="center" prop="moeJtbh" />
      <el-table-column label="生产日期" width="120" align="center" prop="moeScrq" />
      <el-table-column label="生产序号" width="80" align="center" prop="moeScxh" />
      <el-table-column label="生产数量" width="80" align="center" prop="moeScsl" />
      <el-table-column label="良品数量" width="80" align="center" prop="moeLpsl" />
      <el-table-column label="调整数量" width="80" align="center" prop="moeTzsl" />
    </el-table>

    <!-- 下表 -->
    <el-table
      highlight-current-row
      border
      stripe
      id="badAnalyseChild"
      v-loading="loadingChild"
      height="350px"
      :data="badAnalyseChildList"
      :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column label="生产日期" :show-overflow-tooltip="true" width="150" align="center" prop="blmScrq" />
      <el-table-column label="班次" :show-overflow-tooltip="true" width="150" align="center" prop="blmBcdm" />
      <el-table-column label="机台" :show-overflow-tooltip="true" width="150" align="center" prop="blmJtbh" />
      <el-table-column label="不良代码" :show-overflow-tooltip="true" width="150" align="center" prop="blmBldm" />
      <el-table-column label="不良描述" :show-overflow-tooltip="true" width="150" align="center" prop="blmBlms" />
      <el-table-column label="不良数量" :show-overflow-tooltip="true" width="150" align="center" prop="blmBlsl" />
      <el-table-column label="建立人员" :show-overflow-tooltip="true" width="150" align="center" prop="blmJlry" />
      <el-table-column label="建立日期" :show-overflow-tooltip="true" width="150" align="center" prop="blmJlrq" />
    </el-table>

    <!--   查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="700px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-col :span="24">
          <el-form-item label="完工日期">
            <el-date-picker
              v-model="daterangeScrq"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造单号" prop="madeNo">
            <el-input v-model="queryParams.madeNo" placeholder="请输入制造单号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" prop="marketNo">
            <el-input v-model="queryParams.marketNo" placeholder="请输入销售单号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="itemCode">
            <el-input v-model="queryParams.itemCode" placeholder="请输入物料代码" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="综合查询" prop="pmgg">
            <el-input v-model="queryParams.pmgg" placeholder="请输入品名规格或客户编码" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台编号" prop="machineCode">
            <el-input v-model="queryParams.machineCode" placeholder="请输入机台编号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具编号" prop="modelNo">
            <el-input v-model="queryParams.modelNo" placeholder="请输入模具编号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-radio-group v-model="queryParams.state">
            <el-radio :label="'1'">未完成订单</el-radio>
            <el-radio :label="'2'">进行中订单</el-radio>
            <el-radio :label="'3'">已完成订单</el-radio>
            <el-radio :label="'-1'">全部</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { exportBadAnalyse, listBadAnalyse, listBadAnalyseChild } from '@/api/quality/badAnalyse'
import { exportTable } from '@/utils/ruoyi'

export default {
  name: 'BadAnalyseWork',
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingChild: false,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 210,
      showSearch: true,
      badAnalyseList: [],
      badAnalyseChildList: [],
      // 总条数
      total: 0,
      // 机台特性表格数据
      // 弹出层标题
      title: '',
      openSearch: false,
      // 是否显示弹出层
      open: false,
      span: document.createElement('span'),
      spanPage: document.createElement('span'),
      // 建立日期时间范围
      daterangeScrq: [this.$moment().startOf('month').toDate(), this.$moment().add(100, 'year').endOf('month').toDate()],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        dateBegin: null,
        dateEnd: null,
        madeNo: null,
        marketNo: null,
        itemCode: null,
        pmgg: null,
        machineCode: null,
        modelNo: null,
        state: '1',
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSearch() {
      this.reset()
      this.title = '查询'
      this.openSearch = true
    },
    /** 查询 */
    getList() {
      this.loading = true
      if (null != this.daterangeScrq && '' != this.daterangeScrq) {
        this.queryParams.dateBegin = this.moment(this.daterangeScrq[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.dateEnd = this.moment(this.daterangeScrq[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      listBadAnalyse(this.queryParams)
        .then((response) => {
          this.badAnalyseList = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch((error) => {
          console.log(error)
          this.loading = false
        })
    },
    handleClick(row) {
      this.loadingChild = true
      listBadAnalyseChild(row.moeId)
        .then((response) => {
          this.badAnalyseChildList = response.data
          this.loadingChild = false
        })
        .catch((error) => {
          console.log(error)
          this.loadingChild = false
        })
    },
    pageChange(page) {
      this.queryParams.pageNum = page
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        dateBegin: null,
        dateEnd: null,
        madeNo: null,
        marketNo: null,
        itemCode: null,
        pmgg: null,
        machineCode: null,
        modelNo: null,
        state: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExportUp() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出不良品分析数据（上表）？', '警告', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportBadAnalyse(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch((error) => {
          console.log(error)
          this.exportLoading = false
        })
    },
    handleExportDown(id, title) {
      this.$confirm('是否确认导出点击选中此单号的不良品分析数据（下表）？', '警告', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          exportTable(id, title)
          this.exportLoading = false
        })
        .catch((error) => {
          console.log(error)
          this.exportLoading = false
        })
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      // 启动
      if (row.moeZtbz == '1') {
        return 'color:#0fac09'
      }
      // 暂停
      else if (row.moeZtbz == '2') {
        return 'color:#bb0331'
      }
      // 等待
      else if (row.moeZtbz == '3') {
        return 'color:#61b2e0'
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.unit-parent {
  height: 34px;
}

.unit {
  margin-left: 10px;
  height: 34px;
  line-height: 34px;
}
</style>
